//
//  NSString+md5.m
//  MyClouds
//
//  Created by sarah doo on 13-8-12.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "NSString+md5.h"

@implementation NSString (md5)


- (NSString *)trim00{
    
    unichar temp[[self length]];
    NSRange range;
    range.location = 0;
    range.length = [self length];
    [self getCharacters:temp range:range];
    int i=0;
    for (i = 0; i < [self length]; i++) {
        if(temp[i] == 0){
            break;
        }
    }
    range.length = i;
    self = [self substringWithRange:range];
    return self;
    //self = [NSString alloc]initWithCharacters:  length:<#(NSUInteger)#>
}

- (NSString *)addHeadUnicode
{
    NSData* nameData = [self dataUsingEncoding:NSUnicodeStringEncoding];
    Byte bytes[] = {255,254};
    bytes[0] = 255;
    bytes[1] = 254;
    NSMutableData *headData = [[NSMutableData alloc]initWithBytes:bytes length:2];
    [headData appendData:nameData];
    NSString* name = [[NSString alloc] initWithData:headData encoding:NSUnicodeStringEncoding];
    return name;
}

- (NSString *)addHeadASCII{
    NSData* nameData = [self dataUsingEncoding:NSASCIIStringEncoding];
    Byte bytes[] = {255,254};
    bytes[0] = 255;
    bytes[1] = 254;
    NSMutableData *headData = [[NSMutableData alloc]initWithBytes:bytes length:2];
    [headData appendData:nameData];
    NSString* name = [[NSString alloc] initWithData:headData encoding:NSUnicodeStringEncoding];
    return name;
}

-(NSString *) md5HexDigest
{
    if(self == nil || [self length] == 0)
        return nil;
    
    const char *value = [self UTF8String];
    
    
    
    unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
    Byte* bytes[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(value, strlen(value), outputBuffer);
    
    
    NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
        [outputString appendFormat:@"%x",outputBuffer[count]];
    }
    
    return [outputString autorelease];
}

-(NSData *) md5Bytes
{
    if(self == nil || [self length] == 0)
        return nil;
    
    const char *value = [self UTF8String];
    
    Byte* outputBuffer[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(value, strlen(value), outputBuffer);
    
    NSData* data = [NSData dataWithBytes:outputBuffer length:16];
    
    return data;
}

-(char *) md5HexDigesta
{
    if(self == nil || [self length] == 0)
        return nil;
    
    const char *value = [self UTF8String];
    
    unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(value, strlen(value), outputBuffer);
    
    return outputBuffer;
    
}

- (NSString *)quotedPrintableString{
    NSMutableString *encoded = [NSMutableString stringWithCapacity:3*[self length]];
    const char *characters = [self UTF8String];
    NSUInteger length = strlen(characters);
    for (NSUInteger i = 0; i < length; ++i) {
        char character = characters[i];
        int left = character & 0xF;
        int right = (character >> 4) & 0xF;
        [encoded appendFormat:@"=%X%X", right, left];
    }
    return encoded;
}



@end
